//
//  ADTextView.swift
//  ADKit
//
//  Created by Adoma on 2020/11/21.
//

import UIKit
import RxSwift

public class ADTextView: UIView {
    
    public let textView = UITextView().then {
        $0.textColor = ADKit.Color.k333333
        $0.font = UIFont.pingfang(with: 15)
        $0.showsVerticalScrollIndicator = false
        $0.showsHorizontalScrollIndicator = false
        $0.tintColor = ADKit.Color.k666666
    }
    
    public let placeHolderLabel = UILabel().then {
        $0.textColor = ADKit.Color.k999999
        $0.font = UIFont.pingfang(with: 15)
        $0.numberOfLines = 0
    }
    
    public let countLabel = UILabel().then {
        $0.textColor = ADKit.Color.k999999
        $0.font = UIFont.pingfang(with: 15)
    }
    
    public var isEditing: Bool {
        set {
            if newValue {
                self.placeHolderLabel.isHidden = self.textView.text?.count ?? 0 > 0
                self.countLabel.isHidden = self.maxCount == 0
                self.textView.isEditable = true
                self.textView.becomeFirstResponder()
            } else {
                self.placeHolderLabel.isHidden = true
                self.countLabel.isHidden = true
                self.textView.isEditable = false
                self.textView.resignFirstResponder()
            }
        }
        get {
            return self.textView.isFirstResponder
        }
    }
    
    let disposeBag = DisposeBag()
    
    var placeHolder: String?
    var maxCount: Int
    
    public init(placeHolder: String? = nil, maxCount: Int = 0) {
        self.placeHolder = placeHolder
        self.maxCount = maxCount
        super.init(frame: .zero)
        
        placeHolderLabel.isHidden = placeHolder == nil
        countLabel.isHidden = maxCount == 0

        setupUI()
    }
    
    func setupUI() {
        
        backgroundColor = .white
        
        textView.textLimit = maxCount
        addSubview(textView)
        addSubview(placeHolderLabel)
        addSubview(countLabel)

        textView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(8)
            make.left.equalToSuperview().offset(8)
            make.right.equalToSuperview().offset(-8)
            if maxCount > 0 {
                make.bottom.equalTo(self.countLabel.snp.top).offset(-8)
            } else {
                make.bottom.equalToSuperview().offset(-8)
            }
        }
        
        placeHolderLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(16)
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().offset(-16)
        }
        
        countLabel.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-16)
            make.bottom.equalToSuperview().offset(-16)
            make.height.equalTo(20)
        }
        
        if let placeHolder = placeHolder {
            placeHolderLabel.text = placeHolder
            textView.rx.text.orEmpty.map{ $0.count > 0 }.bind(to: placeHolderLabel.rx.isHidden).disposed(by: disposeBag)
        }
        
        if maxCount > 0 {
            textView.rx.text.orEmpty.map{ "\($0.count)/\(self.maxCount)" }.bind(to: countLabel.rx.text).disposed(by: disposeBag)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
